﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using CS_Util;

namespace build {
    public enum ShowCommands {
        SW_HIDE = 0,
        SW_SHOWNORMAL = 1,
        SW_NORMAL = 1,
        SW_SHOWMINIMIZED = 2,
        SW_SHOWMAXIMIZED = 3,
        SW_MAXIMIZE = 3,
        SW_SHOWNOACTIVATE = 4,
        SW_SHOW = 5,
        SW_MINIMIZE = 6,
        SW_SHOWMINNOACTIVE = 7,
        SW_SHOWNA = 8,
        SW_RESTORE = 9,
        SW_SHOWDEFAULT = 10,
        SW_FORCEMINIMIZE = 11,
        SW_MAX = 11
    }

    [System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
    public class SECURITY_ATTRIBUTES {
        public int nLength;
        public string lpSecurityDescriptor;
        public bool bInheritHandle;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct STARTUPINFO {
        public int cb;
        public string lpReserved;
        public string lpDesktop;
        public int lpTitle;
        public int dwX;
        public int dwY;
        public int dwXSize;
        public int dwYSize;
        public int dwXCountChars;
        public int dwYCountChars;
        public int dwFillAttribute;
        public int dwFlags;
        public int wShowWindow;
        public int cbReserved2;
        public byte lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct PROCESS_INFORMATION {
        public IntPtr hProcess;
        public IntPtr hThread;
        public int dwProcessId;
        public int dwThreadId;
    }

    /// <summary>
    /// 配置文件数据类
    /// </summary>
    public class build_config {
        //默认应用名称
        public const string defGameName = "决战传奇";
        public string openTest;//开启测试效果 0:开启  1:关闭  
        public build_config() { }

        public static string _build = "_build";
        public static bool o_isDelectTempFile = false;

        public string o_packageName = "x";
        public string o_gameName = defGameName;//应用名称
        public string o_lastPackageName = "x";
        public string o_keystoreName = "sign.keystore";
        public string o_keystorePass = "123456";
        public string o_keyaliasName = "tejie";
        public string o_keyaliasPass = "123456";
        Dictionary<string, string> configMap = new Dictionary<string, string>();

        public string o_configPath;
        public bool S_LoadData(String currPath) {
            o_configPath = currPath + "build_config.txt";
            C_Log.Log(o_configPath);    
            if (File.Exists(o_configPath) == false) {
                return false;
            }
            string[] list = File.ReadAllLines(o_configPath);
            if (list == null || list.Length < 2) {
                return false;
            }
            for (int i = 0; i < list.Length; i++) {
                string ss = list[i];
                if (ss.Length < 2) {
                    continue;
                }
                string[] sss = ss.Split(new char[] { '=',' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (sss.Length < 2) {
                    continue;
                }
                string key = sss[0].Trim();
                key = S_ClearStringHead(key);
                string value = sss[1].Trim();
                C_Log.Log(key + "__________" + value);
                if (key == "packageName" && value.Length > 1) {
                    o_packageName = value;
                } else if (key == "gameName" && value.Length >= 1) {
                    o_gameName = value;
                } else if (key == "keystoreName" && value.Length >= 1) {
                    o_keystoreName = value;
                } else if (key == "keystorePass" && value.Length > 1) {
                    o_keystorePass = value;
                } else if (key == "keyaliasName" && value.Length > 1) {
                    o_keyaliasName = value;
                } else if (key == "keyaliasPass" && value.Length > 1) {
                    o_keyaliasPass = value;
                } else {
                    configMap[key] = value;
                }
            }
            return true;
        }

        static string S_ClearStringHead(string str) {
            int length = str.Length;
            int errorPosi = -1;
            for (int v = 0; v < str.Length; v++) {
                if (str[v] != 35 && (str[v] < 48 || str[v] > 122)) {
                    //System.out.println("错误......" + chars[v]);
                    errorPosi = v;
                } else {
                    //System.out.println("数据......................." + chars[v]);
                    break;
                }
            }
            if (errorPosi < 0) {
                return str;
            }
            return str.Substring(errorPosi + 1);
        }



        public static void S_FixConfig(String path, build_config setting) {  
            string[] list = File.ReadAllLines(path);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            for (int i = 0; i < list.Length; i++) {
                C_Log.Log("读取原始Config______" + list[i]);
                if (list[i].Length < 2) { 
                    continue;
                }
                string[] sss = list[i].Split('=');
                if (sss.Length < 2) {
                    continue;
                }
                string key = sss[0].Trim();
                key = build_config.S_ClearStringHead(key); 
                dic[key] = sss[1].Trim();
            }
            foreach (var item in dic) {
                if (setting.configMap.ContainsKey(item.Key) == false) {
                    setting.configMap[item.Key] = item.Value;
                }
            }
            StringBuilder data = new StringBuilder();
            foreach (var entry in setting.configMap) {
                C_Log.Log("S_FixConfig______" + entry.Key + "________________" + entry.Value);
                data.Append(entry.Key + "=" + entry.Value + "\r\n");
            }
            File.WriteAllText(path, data.ToString());
        }

    }

}
